# _*_ coding: UTF-8 _*_
# @Time     : 2021/10/28 17:04
# @Author   : yangshuaishuai
# @Site     : http://www.hzdledu.cn/
# @File     : prac.py
# @Software : PyCharm


from c13.mysql_test import db_mysql


class ATM1():
    def __init__(self):
        self.users = None
        self.user = None
        self.index = -1
        self.user_name = None
        self.user_pwd = None
        self.user_balance = -1
        self.read_db()

    def read_db(self):
        self.users = db_mysql('select * from users;')
        self.users = list(self.users)
        for index in range(len(self.users)):
            self.users[index] = list(self.users[index])

    def write_db(self):
        for user in self.users:
            sql = f"UPDATE users SET balance={user[2]} WHERE name='{user[0]}'"
            db_mysql(sql)

    # 登录
    def login(self):
        print('''---------欢迎使用汇智动力ATM----------''')
        isUserExist = False
        name = input('请输入账号：')
        for index in range(len(self.users)):
            if self.users[index][0] == name:
                self.user = self.users[index]
                self.index = index
                isUserExist = True
                self.verify_pwd()
        if isUserExist == False:
            print('账号不存在，请重新登录')
            self.login()

    # 验证密码
    def verify_pwd(self):
        pwd_input = input('请输入密码：')
        if self.user[1] == pwd_input:
            self.main_menu()
        else:
            print('密码错误，请重新输入')
            self.verify_pwd()

    # 主菜单
    def main_menu(self):
        print('''
---------主菜单---------
-1. 查询余额
-2. 取款
-3. 存款
-4. 转账
-5. 退出
------------------------''')
        print(self.users)
        option = input('请输入您要办的业务:')
        if option == '1':
            self.query_balance()
        elif option == '2':
            self.check_out()
        elif option == '3':
            self.save_money()
        elif option == '4':
            self.transfer()
        elif option == '5':
            self.exit()
        else:
            print('您输入了错误的业务，请重新输入')
            self.main_menu()

    # 取钱
    def check_out(self):
        money = int(input('请输入要取得金额:'))
        self.user[2] -= money
        self.users[self.index][2] = self.user[2]
        balanbce = self.user[2]
        print(f'您的账户余额为：{balanbce}元')
        # print(self.users)
        self.write_db()
        self.main_menu()

    # 查询余额
    def query_balance(self):
        balanbce = self.user[2]
        print(f'您的账户余额为：{balanbce}元')
        self.main_menu()

    # 转账
    def transfer(self):
        name = input('请输入要转账的账号:')
        isUserExist = False
        for index in range(len(self.users)):
            if self.users[index][0] == name:
                isUserExist = True
                money = int(input('请输入要转账的金额:'))
                self.user[2] -= money
                self.users[self.index][2] = self.user[2]
                self.users[index][2] += money
                balanbce = self.user[2]
                print(f'您的账户余额为：{balanbce}元')
                self.write_db()
                self.main_menu()
        if isUserExist == False:
            print('您要转账的账号不存在，请重新输入')
            self.transfer()

    # 存钱
    def save_money(self):
        money = int(input('请输入要存入的金额:'))
        self.user[2] += money
        self.users[self.index][2] = self.user[2]
        balanbce = self.user[2]
        print(f'您的账户余额为：{balanbce}元')
        # print(self.users)
        self.write_db()
        self.main_menu()

    # 退出
    def exit(self):
        print('---------欢迎再次使用，再见----------')

    def run(self):
        self.login()


if __name__ == '__main__':
    atm = ATM1()
    atm.run()